/*
 * MongoBaseFragmentActivity.java
 *
 * Created by: WangHongxue on 2014-9-27
 * Copyright (c) 2014��� ������. All rights reserved.
 */
package com.techwells.taco.base;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.techwells.learningkites.R;
import com.techwells.taco.mvvm.BaseFragmentActivity;
import com.techwells.taco.tasktool.TaskToken;
import com.techwells.taco.utils.AnimationUtil;
import com.techwells.taco.utils.CommonUtil;
import com.techwells.taco.utils.ToastUtils;
import com.techwells.taco.widget.CustomProgressDialog;


/**
 * Activity 基础类
 *
 */
public abstract class CommonBaseFragmentActivity extends BaseFragmentActivity {


	public Context context;
	private LinearLayout common_title_leftLayout;
	private ImageView common_title_leftImage;
	private TextView common_title_leftText;
	private TextView common_title_titletext;
	private LinearLayout common_title_rightLayout;
	private ImageView common_title_rightImage;
	private TextView common_title_rightText;

	/**
	 * 初始化页面标题，左边为返回icon，右边无按钮。
	 * 
	 * @param title
	 * @param leftButtonClickListener
	 */
	protected void initTitleBar(int titleResId,
			OnClickListener leftButtonClickListener) {
		String title = getResources().getString(titleResId);
		this.initTitleBar(title, true, false, leftButtonClickListener, null, R.drawable.g_icon_back, -1, null, null);
	}

	/**
	 * 初始化页面标题，左边为返回icon，右边无按钮。
	 * 
	 * @param title
	 * @param leftButtonClickListener
	 */
	protected void initTitleBar(String titleStr,
			OnClickListener leftButtonClickListener) {
		
		this.initTitleBar(titleStr, true, false, leftButtonClickListener, null, R.drawable.g_icon_back, -1, null, null);
	}
	
	/**
	 * 初始化titleBar  左边为返回按钮，右边为文字按钮
	 * 
	 * @param titleResId
	 *            标题显示内容
	 * @param leftButtonClickListener
	 *            头部左边的点击事件
	 * @param rightButtonClickListener
	 *            头部右边的点击事件
	 * @param rightStrRes
	 *            右边按钮显示内容
	 */
	protected void initTitleBar(int titleResId,
			OnClickListener leftButtonClickListener,
			OnClickListener rightButtonClickListener, int rightStrRes) {
		String title = getResources().getString(titleResId);
		String rightStr = getResources().getString(rightStrRes);
		initTitleBar(title, true, true, leftButtonClickListener, rightButtonClickListener, R.drawable.g_icon_back, -1, null, rightStr);
	}


	/**
	 * 初始化titleBar  左边为图片按钮，右边图片按钮
	 * 
	 * @param titleResId
	 *            标题显示内容
	 * @param leftButtonClickListener
	 *            头部左边的点击事件
	 * @param rightButtonClickListener
	 *            头部右边的点击事件
	 * @param hasRightButton是否有右部点击事件
	 * @param leftImageRes左边图片
	 * @param rightImageRes右边图片
	 */
	protected void initTitleBar(String title,
			OnClickListener leftButtonClickListener,
			OnClickListener rightButtonClickListener, 
			int leftImageRes, int rightImageRes) {
		initTitleBar(title, true, true, leftButtonClickListener, rightButtonClickListener, leftImageRes, rightImageRes, null, null);
	}
	
	

	/**
	 * 初始化页面标题，左边按钮，右边按钮只能显示文字或图片。
	 * 
	 * @param title   标题
	 * @param hasLeftButton  是否有左边的按钮
	 * @param hasRightButton  是否有右边的按钮
	 * @param leftButtonClickListener  左边按钮点击事件
	 * @param rightButtonClickListener  右边按钮点击事件
	 * @param leftImageRes  左边按钮显示图片
	 * @param rightImageRes  右边按钮显示图片
	 * @param leftString  左边按钮显示文字
	 * @param rightString  右边按钮显示文字
	 */
	private void initTitleBar(String title,
			boolean hasLeftButton,boolean hasRightButton,
			OnClickListener leftButtonClickListener,OnClickListener rightButtonClickListener,  
			int leftImageRes, int rightImageRes,
			String leftString, String rightString) {
		
		common_title_leftLayout = (LinearLayout) findViewById(R.id.common_activity_title_left_linLayout);
		common_title_leftImage = (ImageView) findViewById(R.id.common_activity_title_left_image);
		common_title_leftText = (TextView) findViewById(R.id.common_activity_title_left_tv);
		common_title_titletext = (TextView) findViewById(R.id.common_activity_title_middle_tv);
		common_title_rightLayout = (LinearLayout) findViewById(R.id.common_activity_title_Right_linLayout);
		common_title_rightImage = (ImageView) findViewById(R.id.common_activity_title_right_iv);
		common_title_rightText = (TextView) findViewById(R.id.common_activity_title_right_tv);

		//左边按钮
		if (hasLeftButton) {
			common_title_leftLayout.setVisibility(View.VISIBLE);
			if (leftImageRes > 0) {// 显示图片
				common_title_leftText.setVisibility(View.GONE);
				common_title_leftImage.setVisibility(View.VISIBLE);
				common_title_leftImage.setImageResource(leftImageRes);
			} else if (leftString != null) {
				common_title_leftText.setVisibility(View.VISIBLE);
				common_title_leftImage.setVisibility(View.GONE);
				common_title_leftText.setText(leftString);
			}
			//左边按钮点击事件
			if (null != leftButtonClickListener) {
				common_title_leftLayout.setOnClickListener(leftButtonClickListener);
				common_title_leftImage.setOnClickListener(leftButtonClickListener);
				common_title_leftText.setOnClickListener(leftButtonClickListener);
			}
		} else {
			common_title_rightLayout.setVisibility(View.INVISIBLE);
		}
		
		//右边按钮
		if (hasRightButton) {
			common_title_rightLayout.setVisibility(View.VISIBLE);
			if (rightImageRes > 0) {// 显示图片
				common_title_rightText.setVisibility(View.GONE);
				common_title_rightImage.setVisibility(View.VISIBLE);
				common_title_rightImage.setImageResource(rightImageRes);
			} else if (rightString != null) {
				common_title_rightText.setVisibility(View.VISIBLE);
				common_title_rightImage.setVisibility(View.GONE);
				common_title_rightText.setText(rightString);
			}
			// 右边按钮点击事件
			if (null != rightButtonClickListener) {
				common_title_rightLayout.setOnClickListener(rightButtonClickListener);
				common_title_rightImage.setOnClickListener(rightButtonClickListener);
				common_title_rightText.setOnClickListener(rightButtonClickListener);
			}
		} else {
			common_title_rightLayout.setVisibility(View.INVISIBLE);
		}
		//title
		common_title_titletext.setText(null == title ? "" : title);
	}
	
	/**
	 * 关闭activity监听
	 */
	protected OnClickListener defaultLeftClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			CommonUtil.KeyBoardCancel(CommonBaseFragmentActivity.this);
			onBackPressed();
		}
	};
	
	@Override
	public void startProgressDialog(boolean hasCancle) {
		if (progressDialog == null) {
			CustomProgressDialog.Builder customBuilder = new CustomProgressDialog.Builder(
					this);
			progressDialog = customBuilder.create();
			progressDialog.setCanceledOnTouchOutside(false);
		}
		ImageView loding_bg = (ImageView) progressDialog.findViewById(R.id.loding_bg);
		AnimationUtil.startRotateAnimation(loding_bg);
		progressDialog.show();

	}

	@Override
	public void requestFailedHandle(TaskToken token, int errorCode,
			String errorMsg) {
		ToastUtils.show(this, errorMsg);
		dismissProgress();
	}

}
